import format from '@dfeidao/atom-web/dt/format';
import parse from '@dfeidao/atom-web/dt/parse';
import get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import p001 from './p001';
import p002 from './p002';
import p006 from './p006';

export default async function a004(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 当点击查询按钮时 调用 并执行一下操作

	// 时间格式
	// 获取日期的值
	const ch_sj = fd.data.node.querySelector<HTMLDataElement>('[data-id="ch-sj"]');
	const ks = parse(ch_sj.value);
	const js_sj = fd.data.node.querySelector<HTMLDataElement>('[data-id="js-sj"]');
	const js = parse(js_sj.value);

	if (ks > js) {
		alert('开始时间大于结束时间');
		return;
	}
	console.log('开始:', ch_sj.value);
	console.log('结束:', js_sj.value);
	// if (ch_sj.value === '') {
	// 	ch_sj.value = '1970-01-01';
	// }
	// if (js_sj.value === '') {
	// 	js_sj.value = '0';
	// }
	// 查询会议列表
	const n = fd.data.node.querySelector<HTMLInputElement>('[data-id="conference_theme"]').value;
	const staff_no = get('staff_no');
	const res = await nodejs<Array<{ meeting_no: string }>>('meeting-management/zj-000001/s001', {
		filter: {
			staff_no
		}
	});
	const arr = Array();
	for (let i = 0; i < res.length; i++) {
		arr.push(res[i].meeting_no);
	}
	const dt = await nodejs<{ dt: Array<{ meeting_no: string, meeting_name: string, start_time: number, conference_theme: string, meeting_status: string, sponsor: string, meeting_address: string }>; c: number }>('meeting-management/zj-000001/s002', {
		filter: {
			meeting_nos: arr,
			pn: 1
		}
	});

	const resdt = await nodejs<{ res: Array<{ meeting_no: string, meeting_name: string, start_time: number, conference_theme: string, meeting_status: string, sponsor: string, meeting_address: string }> }>('meeting-management/zj-000001/s004', {
		conference_theme: n,
		start_time: ks,
		start_time1: js
	});
	const a = Array();
	for (let i = 0; i < dt.dt.length; i++) {
		for (let j = 0; j < resdt.res.length; j++) {
			if (dt.dt[i].meeting_no === resdt.res[j].meeting_no) {
				const s = dt.dt[i].meeting_no;
				a.push(s);
			}
		}
	}
	const ds = await nodejs<{ dt: Array<{ meeting_no: string, meeting_name: string, start_time: number, conference_theme: string, meeting_status: string, sponsor: string, meeting_address: string }>; s: Array<{}> }>('meeting-management/zj-000001/s005', {
		filter: {
			meeting_nos: a,
			pn: 1
		}
	});
	for (let j = 0; j < ds.dt.length; j++) {
		if (ds.dt[j].meeting_status === "1") {
			ds.dt[j].meeting_status = '进行中';
		} else if (ds.dt[j].meeting_status === "2") {
			ds.dt[j].meeting_status = '未解决';
		} else if (ds.dt[j].meeting_status === "3") {
			ds.dt[j].meeting_status = '完成';
		}
	}
	// tslint:disable-next-line:no-shadowed-variable
	const d = ds.dt.map((n) => {
		/* 0 不显示时分秒 1 显示秒  2 不显示秒*/
		return ({ ...n, start_time: format(n.start_time, 0) });
	});
	const dtNO = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p001"]');
	const dtH = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p002"]');
	const dts = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p003"]');
	const dts1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p006"]');
	if (ds.s.length > 0) {
		render(fd, d, p001, 'p001', 'inner');
		dtNO.style.display = 'block';
		dtH.style.display = 'none';
	} else {
		render(fd, '', p002, 'p002', 'inner');
		dtNO.style.display = 'none';
		dtH.style.display = 'block';
	}
	if (d.length > 20) {
		render(fd, ds.s.length, p006, 'p006', 'inner');
		dts.style.display = "none";
		dts1.style.display = "block";
	}


}
